home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP6.TXT < prev    next >
Text File  |  1988-11-27  |  17KB  |  388 lines

  1.             CHAPTER 6 - Arrays, types, constants, and labels
  2.  
  3.  
  4.                                   ARRAYS
  5.  
  6.             Having covered nearly all of the programming statements,
  7.         we  must  now  go  back and fill in some gaps  in  our  data
  8.         definition.
  9.  
  10.            One  of  the most useful data structures  is  the  ARRAY,
  11.         which is,  in the simplest terms,  a group of many identical
  12.         terms.   Lets go directly to an example to see what an array
  13.         looks  like.   Edit the Pascal program ARRAYS and notice the
  14.         third  line  starting  with  the  word  "automobiles".   The
  15.         variable "automobiles" is defined as an integer variable but
  16.         in addition,  it is defined to have twelve different integer
  17.         variables,    namely    "automobile[1]",    "automobile[2]",
  18.         "automobile[3]", .. "automobile[12]".  The square braces are
  19.         used in Pascal to denote a subscript for an array  variable.
  20.         The  array  definition  given  in line  3  is  the  standard
  21.         definition for an array,  namely a variable name followed by
  22.         a  colon and the reserved word ARRAY,  with the range of the
  23.         array given in square brackets followed by another  reserved
  24.         word OF and finally the type of variable.
  25.  
  26.             In using the elements of the array in a program, each of
  27.         the elements of the array are required to be used in exactly
  28.         the same manner as any simple variable having the same type.
  29.         Each  time  one of the variables is used,  it must have  the
  30.         subscript  since the subscript is now part of  the  variable
  31.         name.   The subscript moreover,  must be of the type used in
  32.         the definition and it must be within the range defined or it
  33.         will be listed as an error.
  34.  
  35.             Now  consider the program itself.   As "index" is varied
  36.         from 1 to 12, the range of the variable "automobile", the 12
  37.         variables  are set to the series of values 11  to  22.   Any
  38.         integer values could be used, this was only a convenient way
  39.         to set the values to some numbers.   With the values stored,
  40.         a  header  is  now printed and finally the  list  of  values
  41.         contained in the array.   Note carefully that,  although the
  42.         subscripts are limited to 1 through 12, the values stored in
  43.         each  of  the 12 variables are limited only by the range  of
  44.         integers,  namely -32768 to 32767.  Review this material and
  45.         this program as long as needed to fully understand it, as it
  46.         is very important.
  47.  
  48.                            DOUBLY INDEXED ARRAYS
  49.  
  50.             After understanding the above,  load the program ARRAYS2
  51.         to see the next level of complexity of arrays.  You will see
  52.         that "checkerboard" is defined as an array from 1 to 8,  but
  53.         instead of it being a simple data type, it is itself another
  54.         array from 1 to 8 of INTEGER.   The variable  "checkerboard"
  55.  
  56.  
  57.                                 Page 27
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.             CHAPTER 6 - Arrays, types, constants, and labels
  68.  
  69.  
  70.         is  actually  composed  of 8 elements,  each of which  is  8
  71.         elements,  leading to a total of 64 elements,  each of which
  72.         is  a  simple INTEGER variable.   This is  called  a  doubly
  73.         subscripted  array  and it can be envisioned in exactly  the
  74.         same  manner  as a real checker board,  an 8  by  8  matrix.
  75.         Another  way to achieve the same end is to define the double
  76.         array  as in the next line of the program where  "value"  is
  77.         defined as a total of 64 elements.
  78.  
  79.             To use either of the two variables in a program, we must
  80.         add  two subscripts to the variable name to tell the program
  81.         which  element of the 64 we desire to  use.   Examining  the
  82.         program will reveal two loops,  one nested within the other,
  83.         and both ranging in value from 1 to 8.  The two loop indices
  84.         can  therefore  be used as subscripts of the  defined  array
  85.         variables.   The  variable "checkerboard" is subscripted  by
  86.         both  of  the loop indices and each of the 64  variables  is
  87.         assigned a value as a function of the indices.  The assigned
  88.         value  has  no real meaning other than to illustrate to  you
  89.         how  it is done.   Since the value of "checkerboard" is  now
  90.         available,  it is used to define some values to be used  for
  91.         the variable "value".
  92.  
  93.             After  defining all of those variables,  and you  should
  94.         understand  that we have defined a total of 128 variables in
  95.         the double loop,  they can be printed out.  The next section
  96.         of  the  program does just that,  by  using  another  doubly
  97.         nested  loop,  with a WRITE statement in the  center.   Each
  98.         time  we  go  through the center of the loop we tell  it  to
  99.         print  out  one of the 64 variables  in  the  "checkerboard"
  100.         matrix  with the indices "index" and "count" defining  which
  101.         of  the variables to write each time.   Careful study of the
  102.         loop should reveal its exact operation.
  103.  
  104.             After  printing out the matrix defined by  the  variable
  105.         "checkerboard"  we  still  have the matrix  defined  by  the
  106.         variable  "value"  intact  (In fact,  we still have  all  of
  107.         "checkerboard"  available because we haven't changed any  of
  108.         it).   Before  printing out the matrix defined  by  "value",
  109.         let's  change  a few of the elements just to see how  it  is
  110.         done.   The  next  three  lines simply change three  of  the
  111.         variables  to illustrate that you can operate on all of  the
  112.         matrix  in  loops,  or on any part of the matrix  in  simple
  113.         assignment statements.  Notice especially the third line, in
  114.         which  "value[3,6]" (which was just set to the value of  3),
  115.         is used as a subscript.  This is perfectly legal since it is
  116.         defined as a simple integer variable and is within the range
  117.         of 1 to 8,  which is the requirement for a subscript of  the
  118.         variable  "value".   The  last  part of the  program  simply
  119.         prints  out  the 64 values of the variable "value"   in  the
  120.  
  121.  
  122.  
  123.                                 Page 28
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.             CHAPTER 6 - Arrays, types, constants, and labels
  134.  
  135.  
  136.         same manner as above.  Notice that when you run the program,
  137.         the three values are in fact changed as expected.
  138.  
  139.                             ARRAYS ARE FLEXIBLE
  140.  
  141.             A  few  more words about arrays before we  go  on.   The
  142.         arrays  in the last program were both defined to be  square,
  143.         namely  8 by 8 but that choice was  purely  arbitrary.   The
  144.         subscripts were chosen to go from 1 to 8 but they could have
  145.         been  chosen to go from 101 to 108 or any other range needed
  146.         to clearly define the problem at hand.  And, as you may have
  147.         guessed,  you are not limited to a doubly subscripted matrix
  148.         but you can define a variable with as many subscripts as you
  149.         need  to  achieve your desired end.   There is  a  practical
  150.         limit  to  the  number of subscripts because  you  can  very
  151.         quickly  use up all of your available memory with one  large
  152.         subscripted variable.
  153.  
  154.                             THE TYPE DEFINITION
  155.  
  156.             Now  that  you understand arrays,  lets look at  a  more
  157.         convenient  way to define them by examining the Pascal  file
  158.         TYPES.   You  will notice a new section at the beginning  of
  159.         the listing with the heading TYPE.  TYPE is another reserved
  160.         word  which  is used at the beginning of a section  used  to
  161.         define  "user-defined  types".   Beginning with  the  simple
  162.         predefined TYPES we studied earlier, we can build up as many
  163.         new  types  as  we need and they can be  as  complex  as  we
  164.         desire.   The  six names (from "array_def" to "boat") in the
  165.         TYPE section are not variables,  but are defined to be TYPES
  166.         and  can be used in the same manner as  can  INTEGER,  BYTE,
  167.         REAL, etc.
  168.  
  169.             This  is a very difficult concept,  but a very important
  170.         one.   The Pascal compiler is very picky about the  variable
  171.         types  you  use in the program,  doing lots of  checking  to
  172.         insure  that  you do not use the wrong type anywhere in  the
  173.         program.   Because  it is picky,  you could do  very  little
  174.         without  the  ability to define new types when  needed,  and
  175.         that is the reason that you can define new types to solve  a
  176.         particular problem.
  177.  
  178.             Some of these types are used in the VAR declaration part
  179.         of the program.  Notice that since "airplane" is an array of
  180.         "dog_food"  and  "dog_food" is in turn an array of  BOOLEAN,
  181.         then  "airplane" defines a doubly  subscripted  array,  each
  182.         element being a boolean variable.   This does not define any
  183.         variables, only a TYPE, which can be used in a VAR to define
  184.         a matrix of boolean variables.   This is in fact done in the
  185.         definition of "puppies", which is an array composed of 72 (6
  186.         times 12) boolean variables.  In the same manner, "stuff" is
  187.  
  188.  
  189.                                 Page 29
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.             CHAPTER 6 - Arrays, types, constants, and labels
  200.  
  201.  
  202.         composed of an array of 14 variables,  each being an integer
  203.         variable.   The  elements  of the  array  are,  "stuff[12]",
  204.         "stuff[13]",  ..  "stuff[25]".  Notice also that "stuff2" is
  205.         also defined in exactly the same manner and is also composed
  206.         of 14 variables.
  207.  
  208.             Careful  inspection  will  reveal that  "kitties"  is  a
  209.         variable  which  has the same definition as  "puppies".   It
  210.         would  probably be poor programming practice to define  them
  211.         in  different  manners  unless they  were  in  fact  totally
  212.         disassociated.   In  this  example  program,  it  serves  to
  213.         illustrate  some  of  the  ways user-defined  types  can  be
  214.         defined.
  215.  
  216.             In  a tiny program like this example,  the value of  the
  217.         TYPE declaration part cannot be appreciated,  but in a large
  218.         program  with many variables,  the TYPE declaration  can  be
  219.         used to great advantage.  This will be illustrated later.
  220.  
  221.                          THE CONSTANT DECLARATION
  222.  
  223.             Examining  the Pascal example program CONSTANT will give
  224.         us an example of a constant definition.   The reserved  word
  225.         CONST is the beginning of the section that is used to define
  226.         constants  that can be used anyplace in the program as  long
  227.         as  they  are  consistent  with  the  required  data  typing
  228.         limitations.   In  this example,  "max_size" is defined as a
  229.         constant  with the value of 12.  This is not a variable  and
  230.         cannot  be  changed  in the program,  but is  still  a  very
  231.         valuable  number.   For  the  moment  ignore  the  next  two
  232.         constant definitions.   As we inspect the TYPE declarations,
  233.         we see two user-defined types,  both of which are arrays  of
  234.         size  1 to 12 since "max_size" is defined as 12.   Then when
  235.         we get to the VAR declaration part,  we find five  different
  236.         variables,  all  defined  as arrays from 1 to 12  (some  are
  237.         INTEGER and some are CHAR).   When we come to the program we
  238.         find  that  it is one big loop which we go through 12  times
  239.         because the loop is executed "max_size" times.
  240.  
  241.             In the above definition,  there seems to be no advantage
  242.         to  using the constant,  and there is none,  until you  find
  243.         that  for some reason you wish to increase the range of  all
  244.         arrays from 12 to 18.   In order to do so,  you only need to
  245.         redefine the value of the constant, recompile, and the whole
  246.         job  is done.   Without the constant definition,  you  would
  247.         have had to change all TYPE declarations and the upper limit
  248.         of the loop in the program.  Of course that would not be too
  249.         bad in the small example program,  but could be a real  mess
  250.         in  a 2000 line program,  especially if you missed  changing
  251.         one  of the 12's to an 18.   That would be a good example of
  252.         data in and garbage out.   That should give you a good  idea
  253.  
  254.  
  255.                                 Page 30
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.             CHAPTER 6 - Arrays, types, constants, and labels
  266.  
  267.  
  268.         of  what  the constant can be used for,  and as you  develop
  269.         good  programming  techniques,  you will  use  the  constant
  270.         declaration to your advantage.
  271.  
  272.                       THE TURBO PASCAL TYPED CONSTANT
  273.  
  274.             We   skipped   over  the  second  and   third   constant
  275.         declaration  for a very good reason,  they are not  constant
  276.         declarations.   TURBO  Pascal has defined,  as an extension,
  277.         the "typed constant".  Using the syntax shown, "index_start"
  278.         is defined as an INTEGER variable and is initialized to  the
  279.         value  of  49.   This is a true variable and can be used  as
  280.         such  in the program.   The same effect can be  achieved  by
  281.         simply  defining  "index_start"  as an INTEGER  in  the  VAR
  282.         declaration  part  and setting it to the value of 49 in  the
  283.         program itself.  Since it does not really fit the definition
  284.         of  a  constant,  it's  use is discouraged  until  you  gain
  285.         experience  as  a Pascal programmer.   Until  then  it  will
  286.         probably  only  be  confusing  to  you.    In  like  manner,
  287.         "check_it_out"  is  a  boolean variable initialized  to  the
  288.         value "true".  It is not a constant.
  289.  
  290.                            THE LABEL DECLARATION
  291.  
  292.             Finally,  the example program LABELS will illustrate the
  293.         use  of  labels.   In the Pascal definition,  a LABEL  is  a
  294.         number from 0 to 9999 that is used to define a point in  the
  295.         program  to  which  you wish to jump.   All labels  must  be
  296.         defined  in the LABEL definition part of the program  before
  297.         they can be used.   Then a new reserved word GOTO is used to
  298.         jump to that point in the program.   The best way to see how
  299.         the  GOTO  is  used with labels is to  examine  the  program
  300.         before you.   TURBO Pascal has an extension for labels.  Any
  301.         valid identifier, such as used for variables, can be used as
  302.         a label in addition to the values from 0 to 9999.  These are
  303.         illustrated in the example program.
  304.  
  305.                              THE PACKED ARRAY
  306.  
  307.             When  Pascal  was first defined in  1971,  many  of  the
  308.         computers in use at that time used very large words, 60 bits
  309.         being  a typical word size.   Memory was very expensive,  so
  310.         large memories were not too common.   A Pascal program  that
  311.         used  arrays  was inefficient because only one variable  was
  312.         stored  in each word.   Most of the bits in each  word  were
  313.         totally  wasted,  so  the PACKED ARRAY was defined in  which
  314.         several  variables  were stored in each  word.   This  saved
  315.         storage space but took extra time to unpack each word to use
  316.         the data.  The programmer was given a choice of using a fast
  317.         scheme  that wasted memory,  the ARRAY,  or a slower  scheme
  318.         that used memory more efficiently, the PACKED ARRAY.
  319.  
  320.  
  321.                                 Page 31
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.             CHAPTER 6 - Arrays, types, constants, and labels
  332.  
  333.  
  334.  
  335.             The modern microcomputer has the best of both schemes, a
  336.         short word, usually 16 bits, and a large memory.  The PACKED
  337.         ARRAY  is therefore not even implemented in  many  compilers
  338.         and will be ignored during compilation.
  339.  
  340.  
  341.                            PROGRAMMING EXERCISES
  342.  
  343.         1.  Write  a program to store the integers 201 to 212 in  an
  344.             array then display them on the monitor.
  345.  
  346.         2.  Write a program to store a 10 by 10 array containing the
  347.             products  of  the indices,  therefore  a  multiplication
  348.             table. Display the matrix on the video monitor.
  349.  
  350.         3.  Modify  the program in 2 above to include a constant  so
  351.             that  by simply changing the constant,  the size of  the
  352.             matrix and the range of the table will be changed.
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                 Page 32
  388.